package com.andaily.web.controller.developer;

import com.andaily.domain.dto.developer.BurnDownDetailsDto;
import com.andaily.domain.dto.developer.CheckSprintFinishDto;
import com.andaily.domain.dto.developer.CheckSprintStartDto;
import com.andaily.domain.dto.developer.SprintBacklogsDto;
import com.andaily.service.ScrumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Date: 13-8-21
 *
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/sprint_action")
public class SprintActionController {

    @Autowired
    private ScrumService scrumService;

    @RequestMapping("check_start/{guid}")
    public String checkStart(@PathVariable("guid") String guid, Model model) {
        CheckSprintStartDto checkSprintStartDto = scrumService.loadCheckSprintStartDto(guid);
        model.addAttribute("checkSprintStartDto", checkSprintStartDto);
        return "sprint_check_start";
    }

    @RequestMapping("load_backlogs/{guid}")
    public String loadBacklogs(@PathVariable("guid") String guid, Model model) {
        SprintBacklogsDto sprintBacklogsDto = scrumService.loadSprintBacklogsDto(guid);
        model.addAttribute("sprintBacklogsDto", sprintBacklogsDto);
        return "sprint_backlogs";
    }

    @RequestMapping("start/{guid}")
    public String start(@PathVariable("guid") String guid) {
        String sprintGuid = scrumService.startSprint(guid);
        return "redirect:../..?currentSprint.guid=" + sprintGuid + "&status=CREATED&alert=startSprintSuccess";
    }

    @RequestMapping("archive/{guid}")
    public String archive(@PathVariable("guid") String guid, String to) {
        scrumService.archiveSprint(guid);
        String url = "sprint".equalsIgnoreCase(to) ? "../../sprint_overview" : "../..";
        return "redirect:" + url + "?alert=archiveSprintSuccess";
    }

    @RequestMapping("check_finish/{guid}")
    public String checkFinish(@PathVariable("guid") String guid, Model model) {
        CheckSprintFinishDto checkSprintFinishDto = scrumService.loadCheckSprintFinishDto(guid);
        model.addAttribute("checkSprintFinishDto", checkSprintFinishDto);
        return "sprint_check_finish";
    }

    @RequestMapping("finish/{guid}")
    public String finish(@PathVariable("guid") String guid, String to) {
        scrumService.finishSprint(guid);

        String url = "sprint".equalsIgnoreCase(to) ? "../../sprint_overview?" : "../..?currentSprint.guid=" + guid + "&";
        return "redirect:" + url + "alert=finishSprintInfo";
    }

    @RequestMapping("burndown/details/{guid}")
    public String burnDownDetails(@PathVariable("guid") String guid, Model model) {
        BurnDownDetailsDto burnDownDetailsDto = scrumService.loadBurnDownDetailsDto(guid);
        model.addAttribute("burnDownDetailsDto", burnDownDetailsDto);
        return "burndown_details";
    }


}
